﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using Ext.Net;
using Sharp.Infrastructure.Search;
using Sharp.Portal.ExtNet;


namespace Ext.Net
{
    /// <summary>
    /// ExtStore扩展
    /// </summary>
    public static class ExtStoreExtension
    {
        /// <summary>
        /// 转换为PagingFeature
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static PagingFeature ToPagingFeature(this ExtRequestParameters parameter)
        {
            PagingFeature pf = new PagingFeature(UIEngine.Ext);

            var nameValue = new NameValueCollection
            {
                HttpContext.Current.Request.Form,
                HttpContext.Current.Request.QueryString
            };

            StringBuilder sb = new StringBuilder();
            foreach (string key in nameValue.Keys)
            {
                if (key.StartsWith("_"))
                    continue;
                if (key.StartsWith("Optimizes["))
                    continue;
                sb.AppendFormat("{0}={1}&", key, HttpUtility.UrlEncode(nameValue[key]));
            }

            pf.Optimizes = parameter.Optimizes;
            pf.SimpleSort = string.Format("{0} {1}", parameter.SimpleSort, parameter.SimpleSortDirection);
            pf.PageNo = parameter.Page;
            pf.PageSize = parameter.Limit;

            pf.RequestString = sb.ToString().TrimEnd('&');

            return pf;
        }
    }
}